package com.ysx.server.controller.admin;
import java.time.LocalDateTime;

import com.github.pagehelper.Page;
import com.ysx.server.mapper.GoodsMapper;
import com.ysx.server.mapper.UserMapper;
import com.ysx.server.pojo.dto.OrderPageQueryDTO;
import com.ysx.server.pojo.entity.Goods;
import com.ysx.server.pojo.entity.Orders;
import com.ysx.server.pojo.entity.User;
import com.ysx.server.pojo.vo.OrdersVO;
import com.ysx.server.pojo.vo.PageQueryVO;
import com.ysx.server.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/order")
@Slf4j
public class OrderManageController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询
     * @param orderPageQueryDTO
     * @return
     */
    @GetMapping("/order")
    public PageQueryVO<OrdersVO> orderPageQuery(OrderPageQueryDTO orderPageQueryDTO){
        log.info("查询订单：{}",orderPageQueryDTO);
        if(orderPageQueryDTO==null){
            return null;
        }
        Page<Orders> page = orderService.page(orderPageQueryDTO);
        long total = page.getTotal();
        List<Orders> result = page.getResult();
        List<OrdersVO> ordersVOS = new ArrayList<>();
        result.forEach(order->{
            Goods goods = goodsMapper.getById(order.getGoodsId());
            User user = userMapper.getById(order.getUserId());
            OrdersVO ordersVO = new OrdersVO();
            ordersVO.setId(order.getId());
            ordersVO.setOrderId(order.getOrderId());
            ordersVO.setGoodsName(goods.getGoodsName());
            ordersVO.setUserAccount(user.getUserAccount());
            ordersVO.setOrderStatus(order.getOrderStatus());
            ordersVO.setCheckoutTime(order.getCheckoutTime());
            ordersVOS.add(ordersVO);
        });
        return new PageQueryVO<OrdersVO>(total,ordersVOS);
    }
}
